package org.test.rocketmq.producer03;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * 延迟消息。RocketMQ默认支持18个等级的延迟投递，不满足需求可以自己修改
 */
public class ScheduledMessageProducerExample {

    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("ExampleScheduledMessageGroup");
        producer.setNamesrvAddr("localhost:9876");
        producer.start();
        int totalMessagesToSend = 10;
        long now = System.currentTimeMillis();
        for (int i = 0; i < totalMessagesToSend; i++) {
            Message message = new Message("TopicTest", ("Hello scheduled message " + i).getBytes());
            message.setKeys("ScheduledKey-" + now + "-" + i);
            // This message will be delivered to consumer 60 seconds later.
            message.setDelayTimeLevel(3);
            // Send the message
            producer.send(message);
        }

        // Shutdown producer after use.
        producer.shutdown();
    }
}
